package com.tensquare.base.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import com.tensquare.dto.PageResult;
import com.tensquare.dto.Result;
import com.tensquare.dto.StatusCode;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@CrossOrigin //解决跨域的问题
@RestController
@RequestMapping("/label")
public class LabelController {

    @Autowired
    private LabelService labelService;
    
    //标签的条件分页查询
    @PostMapping("/search/{page}/{size}")
    public Result search(@PathVariable int page,@PathVariable int size,@RequestBody Label label) {
    	
    	//查询
    	Page<Label> pageBean = labelService.search(page,size,label);
    	//返回
    	return new Result(
    			true,
    			StatusCode.OK,
    			"条件分页查询成功",
    			new PageResult<Label>(pageBean.getTotalElements(),pageBean.getContent())
    		);
    }
    
    //标签的条件查询
    @PostMapping("/search")
    public Result search(@RequestBody Label label) {
    	System.out.println("查询条件："+label);
    	//查询
    	List<Label> labelList = labelService.search(label);
    	//返回
    	return new Result(true,StatusCode.OK,"条件查询成功",labelList);
    }
    

    //查询所有
    @GetMapping
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询所有标签成功",labelService.findAll());
    }
    //查询一个
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询一个标签成功",labelService.findById(id));
    }
    //新增一个
    @PostMapping
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true,StatusCode.OK,"新增标签成功");
    }
    //修改一个
    @PutMapping("/{id}")
    public Result update(@PathVariable String id, @RequestBody Label label){
        label.setId(id);//以url的id为主
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改标签成功");
    }
    //删除一个
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除标签成功");
    }

}
